﻿using System;
using System.Collections.Generic;
using System.Reflection;

namespace Aeon.Configuration
{
    /// <summary>
    /// Specifies a series of valid arbitrary values for a property or field.
    /// </summary>
    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
    public class DiscreteDomainConstraintAttribute : Attribute
    {
        private readonly object[] domains;

        /// <summary>
        /// Initializes a new instance of the <see cref="DiscreteDomainConstraintAttribute"/> class.
        /// </summary>
        protected DiscreteDomainConstraintAttribute()
        {
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="DiscreteDomainConstraintAttribute"/> class.
        /// </summary>
        /// <param name="domains">Specifies the valid domains.</param>
        public DiscreteDomainConstraintAttribute(params object[] domains)
        {
            this.domains = domains;
        }

        /// <summary>
        /// Returns a collection of the valid domains for the property or field.
        /// </summary>
        /// <param name="member">Property or field to which the constraint applies.</param>
        /// <param name="context">Device configuration instance which contains the property.</param>
        /// <returns>Collection of the valid domains for the property or field.</returns>
        public virtual IEnumerable<object> GetDomains(MemberInfo member, IDeviceConfiguration context)
        {
            if(this.domains == null)
                return new object[0];

            return Array.AsReadOnly(this.domains);
        }
    }
}
